---
layout : doc
title : ViewModels / MVVM
permalink: docs\ViewModels.html
---

## MVVM Capabilities

Like any other MVVM framework, Phoenix has a few MVVM features to help you keep your viewmodel logic in your viewmodels, and help keep them testable.

All ViewModels in Phoenix must inherit from `ViewModelBase` in the Phoenix framework, that is because to give you all the MVC goodness, the viewmodels need to know the context they are hosted in, and services are also injected into the base class for you to use.

The base INotifyPropertyChanged implementation comes from the NotifyPropertyChanged class, and is fully supported by NotifyPropertyWeaver so you can simply use automatic properties. If you have models that just need notify property changed support, use this class, not ViewModelBase, because it is much lighter.

Phoenix also contains a DelegateCommand class to help with commanding.

If there are MVVM features you would like to see in Phoenix, email me at jake at ginnivan.net with suggestions.

## Invoking actions from view/viewmodel

To help with the MVC side of Phoenix, there are helpers, and shortcuts for invoking controller actions from both the view directly, and the viewmodel.

### From the view

The Invoke helper class is your friend in Phoenix, if you would like a button to invoke a controller action directly, you can use the following xaml:

    <Button Invoke.OnClick="MyController.Action" />

If you have a list, and want to invoke a specific action you can do something like:

    <ListBox>
        <ListBoxItem Invoke.OnSelection="MyController.SomeAction" />
        <ListBoxItem Invoke.OnSelection="MyController.AnotherAction" />
    </ListBox>
    
If you have a bound list, you can always go:

    <ListBox Invoke.OnItemSelected="MyController.SomeAction"
                    ItemSource="{Binding MyModels}" />
    
    public ActionResult SomeAction(MyModel dataContext)
    {
    
    }
    
Phoenix will automatically pass the data context of the selected item to the controller action if it is a parameter.

### From the view model

There are a few helpers in the viewmodel, the Controller<TController> helper in the base class is the best way to invoke actions in a type safe manner.

    Controller<HomeController>().Invoke(c=>c.MyAction(someVariable));
    
**NOTE:** Due to Windows Phone 7 platform constraints, you cannot pass LOCAL variables in this way to controller actions. Phoenix uses expression trees and on WP7 they cannot be compiled, so Phoenix manually parses them and invokes the controller action via reflection. Local variables are treated as *private* variables, and because apps do not run as full trust, we cannot access any non-public variables via reflection.
This means that you should use the non type safe overloads, or make sure you only pass public properties or constants to the controller action.

#### Different overloads:

    Controller<HomeController>().Invoke(c => c.MyAction(someVariable)); //Type safe approach
    Controller<HomeController>().Invoke("MyAction", new object[]{someVariable}); // same as above but not type safe
    InvokeActionBuilder.Invoke("Home.MyAction", someVariable); //Same as above, but non type safe
    Controller<HomeController>().InvokeCommand(c=>c.MyAction(someVariable)); //Returns an ICommand which will invoke the controller action
    await Controller<HomeController>().InvokeActionWithResult<TResult>(c=>c.MyAction(someVariable)); // Will return a Task<TResult> which you can register a continuation or await for the result!
    
There are a heap of overloads, each allowing you to optionally specify a target frame (see [navigation frames](NavigationFrames.html)) and also custom Invoke Options (see [controllers](Controllers.html)). Invoke, InvokeCommand and InvokeWithResults all have the same number of overloads (that make sense).